function unset(array, value){
	var output=[];
	for(var i in array){
		if (array[i]!=value)
			output[i]=array[i];
	}
	return output;
}
function displayType(item, id){
	switch (item.type) {
	case "text":
		return ('<input type="text" id="'+id + item.name+'" name = "'+id + item.name+'"/>')
		break;
	case "combo":
		var out ='';
		$.ajax({
			  url: item.comboUrl,
			  dataType: 'json',
			  async: false,
			  success: function(data) {
			    out+='<select id="'+id + item.name+'" name = "'+id + item.name+'">';
			    $.each(data, function(i,elmt) {
			    	out += '          <option value="'+elmt.value+'">'+elmt.libelle+'</option>';
			    	});
			    out +='</select>';
			  }
			});
		return (out);
		break;

	default:
		return ('<input type="text" id="'+id + item.name+'" name = "'+id + item.name+'"/>')
		break;
	}
}
(function( $ ){
	function selectionne (id, item){
		if ($("#"+id).hasClass("selecti")){
			$("#"+id).removeClass("selecti");
			isSelected = unset(isSelected, item);
		}else{			
			isSelected.push(item);
			$("#"+id).addClass("selecti");
		}
	}
	$.fn.abGrid = function(options) {

		var settings = {
				id : Math.round(10000*Math.random()),
				name : '',
				beforeEdit : null,
				beforeDelete : function(){alert('Etes vous surs de vouloir supprimer l\'enregistrement')},
				afterEdit : null,
				afterDelete : null
				
		};
		var isSelected = new Array();;
		var load = null;

		return this.each(function() {        
			container=$(this);
			//alert (container.attr('id'));
			container.data("isSelected", new Array());
			container.data("isModifying", false);
			if ( options ) { 
				$.extend( settings, options );
			}
			load = function(){
				$.ajax({
				type: "POST",
				dataType: "json",
				url: settings.url_list,

				success: function(data){

				var out ='<div class="arrondi containerGrid" >';
				out += '<p align="right" id="menu'+settings.id+'"><a id="'+settings.id+'AddLn" href="#"><img src="'+baseURL+'/images/ajouter-icone-grand.png" /></a><a id="'+settings.id+'SuppLn" href="#"><img src="'+baseURL+'/images/supp-icone.png" style ="margin-left : 20px; margin-right : 10px;" /></a></p><table width="100%" border="0"  > <tr class="headerTr" bgcolor="#FFFFFF">';
				for (cols in settings.cols_config){
					out += '<td width="'+settings.cols_config[cols].width+'">'+settings.cols_config[cols].libelle+'</td>';

				}
				out +=  '</tr>';
				$.each(data, function(i,item) {

					if (i%2 == 0){
						out += "<tr  class = \"row\" bgcolor=\"#E4FFCA\" id = \""+ settings.id + item.id+"\" item = \""+ item.id+"\" >";
						for (cols in settings.cols_config){
							out += " <td class = \"data\" id = \"td_" + settings.cols_config[cols].name +"_"+ item.id+ "\" attrib = " + settings.cols_config[cols].name +" valeur = "+ item.cell[settings.cols_config[cols].name] +"  >" + item.cell[settings.cols_config[cols].name] + "</td>";
						}
						out += "</tr>";
					} else
					{
						out += "<tr  class = \"row\" bgcolor=\"#CFE4FA\" id = \""+ settings.id + item.id+"\" item = \""+ item.id+"\" >";
						for (cols in settings.cols_config){
							out += " <td class = \"data\" id = \"td_" + cols.name +"_"+ item.id+ "\" attrib = " + cols.name +" valeur = "+ item.cell[cols.name] +"  >" + item.cell[cols.name] + "</td>";
						}
						out += "</tr>";
					}
				});
				out += "</table>";
				out += '<div id="'+settings.id+'Dialog" title="Nouveau '+settings.name+'">';
				out += '		<form id ="'+settings.id+'Form"  method="post" enctype="application/x-www-form-urlencoded" >';
				out += '		   <table class ="formTable" border="0">';
				$.each(settings.add_config, function(i, item){
					out += '			  <tr>';
					out += '			    <td>'+item.libelle+'</td>';
					out += '			    <td>'+displayType(item, settings.id)+'</td>';
					out += '			  </tr>';
				})
				out += '			  <tr>';
				out += '			    <td><label>';
				out += '			      <input type="submit" name="Submit" class = "submit" value="Valider" />';
				out += '			    </label></td>';
				out += '			    <td>&nbsp;</td>';
				out += '			  </tr>';
				out += '			</table>';
				out += '		</form>	';
				out += '</div>';
				out += "</div>";
				//alert (out);
				container.html(out);
				$('#'+settings.id+'Dialog').dialog({autoOpen: false});
				$(".row", container).click(function(){
					var item = $(this).attr("item");
					var isSelected = container.data("isSelected");
					if ($(this).hasClass("selecti")){
						$(this).removeClass("selecti");
						isSelected = unset(isSelected, item);
					}else{			
						isSelected.push(item);
						$(this).addClass("selecti");
					}
					container.data("isSelected", isSelected);
				})
				
				$(".data", container).dblclick(function(){
					if (!container.data("isModifying")){
						var out ='' ;
						var attrib = $(this).attr('attrib');
						var valeur = $(this).attr('valeur');
						var id = $(this).attr('id');
						container.data("isModifying", true);
						out +=  "   <input type = text name = \"" + attrib + "\" id = \""+ attrib + "_" + id + "_edit\" id_val = \"" + id + "\" value = \""+ valeur +"\" />";
						$(this).html(out);
						$("#" + attrib + "_" + id + "_edit").focus();
						$("#"+ attrib + "_" + id + "_edit").focusout(function(){
							$.post(settings.url_edit, { id: $(this).attr('id_val'), attrib: $(this).attr('name'), valeur : $(this).val() },function(data){
								load.call(container);
								$.isFunction(settings.afterEdit) && settings.afterEdit.call(container);
							} );
							container.data("isModifying", false);
						})

						
					}
				});
				
				$("#"+settings.id+"AddLn").click(
						function(){
							$('#'+settings.id+'Dialog').dialog("open");
						}
						);
				
				
			}
			});
			}
			load.call(this);

		});

	};
})( jQuery );